package com.vitabuybuy.tv.ui.mine.talent.usercenter

import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AlertDialog
import androidx.core.content.ContextCompat
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.databinding.FragmentUserCenterBinding
import com.vitabuybuy.tv.dto.TalentUserInfoDto
import com.library.common.base.BaseFragment
import com.library.common.base.MessageModel
import com.library.common.ext.setOnclickNoRepeat
import com.library.common.toast.XToast
import com.vitabuybuy.tv.adaptebinding.ImageAdapter
import com.vitabuybuy.tv.ui.mine.talent.adapter.CaseAdapter
import com.vitabuybuy.tv.ui.mine.talent.usercenter.adapter.TalentCaseAdapter
import com.vitabuybuy.tv.ui.mine.talent.usercenter.viewmodel.UserCenterViewModel
import com.vitabuybuy.tv.utils.StringUtil
import kotlinx.android.synthetic.main.fragment_user_center.*

class UserCenterFragment : BaseFragment<UserCenterViewModel, FragmentUserCenterBinding>() {
    override val layoutId: Int
        get() = R.layout.fragment_user_center

    private val caseAdapter by lazy { TalentCaseAdapter() }

    override fun initView(savedInstanceState: Bundle?) {
        mBinding.viewModel = viewModel
        lvCase.apply {
            adapter = caseAdapter
            layoutManager = LinearLayoutManager(mContext)
        }
        viewModel.isFirstLoading.set(true)
    }

    override fun onResume() {
        super.onResume()
        viewModel.getTalentUserInfo()
    }

    override fun lazyLoadData() {
        viewModel.applyTalentUserInfo.observe(this, Observer {
            ImageAdapter.setCircleImageUrl(
                ivAvatar,
                it.avatar,
                ContextCompat.getDrawable(mContext, R.drawable.wode_icon_touxiang)
            )
            tvName.text = it.realname
            tvSex.text = it.gender
            tvFans.text = String.format(getString(R.string.fans_1), it.favoriteQuantity)
            tvAge.text = String.format(getString(R.string.age), it.age)
            it.regionList?.run {
                mBinding.tvLocation.text = if (this.isNotEmpty()) this[0].name else ""
            }
            it.labelList?.run {
                var labels = ""
                for (lable in this) {
                    labels = labels + lable.name + "、"
                }
                mBinding.tvExpertArea.text = String.format(
                    getString(R.string.expert_area),
                    if (labels.isNotEmpty()) labels.substring(0, labels.length - 1) else "无"
                )
            }
            tvCommissionRatio.text =
                String.format(getString(R.string.commission_ratio_1), it.shareTaskRate)
            tvCommissionRatio2.text =
                String.format(getString(R.string.commission_ratio_2), it.liveTaskRate)
            it.experienceList?.run {
                caseAdapter.setNewData(this)
            }
            btnAddNewCase.visibility = if (caseAdapter.mData.size >= 5) {
                View.GONE
            } else {
                View.VISIBLE
            }
            tvFacebookName.text = "FaceBook"
            tvFacebookId.text = String.format(getString(R.string.id_2), it.facebookId)
            tvFacebookFans.text = String.format(getString(R.string.fans_1), it.facebookFansQuantity)

            tvInstagramName.text = "Instagram"
            tvInstagramId.text = String.format(getString(R.string.id_2), it.instagramId)
            tvInstagramFans.text =
                String.format(getString(R.string.fans_1), it.instagramFansQuantity)

            tvTwitterName.text = "Twitter"
            tvTwitterId.text = String.format(getString(R.string.id_2), it.twitterId)
            tvTwitterFans.text = String.format(getString(R.string.fans_1), it.twitterFansQuantity)
        })
    }

    override fun eventHandling() {
        setOnclickNoRepeat(ivWallet, ivIncomeRecord, ivLiveReward, btnEdit, btnAddNewCase) { v ->
            when (v) {
                ivWallet -> TalentWalletActivity.openActivity(mContext)
                ivIncomeRecord -> TalentWithdrawRecordActivity.openActivity(mContext, getString(R.string.income))
                ivLiveReward -> LiveIncomeManageActivity.openActivity(mContext)
                btnEdit -> TalentEditInfoActivity.openActivity(mContext)
                btnAddNewCase -> EditOrAddNewCaseActivity.openActivity(mContext, null)
            }
        }
        caseAdapter.onEditCaseListener = {
            EditOrAddNewCaseActivity.openActivity(mContext, caseAdapter.mData[it])
        }
        caseAdapter.onDeleteCaseListener = { position ->
            AlertDialog.Builder(mContext)
                .setTitle(getString(R.string.logout))
                .setMessage(getString(R.string.are_you_sure_delete_this_case))
                .setCancelable(true)
                .setPositiveButton(getString(R.string.confirm)) { _, _ ->
                    viewModel.deleteCase(caseAdapter.mData[position].id, position)
                }
                .setNegativeButton(getString(R.string.cancel)) { _, _ -> }.create()
                .show()
        }
    }

    override fun handleEvent(msg: MessageModel) {
        when (msg.code) {
            1 -> {
                XToast.success(getString(R.string.already_delete))
                caseAdapter.removeAt(msg.arg1)
                if (caseAdapter.mData.size < 5) {
                    btnAddNewCase.visibility = View.VISIBLE
                }
            }
        }
    }

}